/**
 * @file Beep.c
 * @author Jasper
 * @brief
 * @version 0.1
 * @date 2024-12-10
 *
 * @copyright Copyright (c) 2024
 *
 */
#include "Beep.h"
#include "sc32f1xxx_tim.h"
#include "sc32f1xxx.h"
unsigned int Volume = 50;
uint16_t beat = 0;
unsigned char Tmp_Count = 0, Tmp_Bit = 0, Music_Enable = 0;
Typedef_Song Global_Song;
const unsigned int tone[] = {
    1046 / 2, // mid  1  (0)
    1174 / 2, // mid  2  (1)
    1318 / 2, // mid  3  (2)
    1396 / 2, // mid  4  (3)
    1568 / 2, // mid  5  (4)
    1760 / 2, // mid  6  (5)
    1976 / 2, // mid  7  (6)
    2094 / 2, // high 1 (7)
    2350 / 2, // high 2 (8)
    2638 / 2, // high 3 (9)
    2794 / 2, // high 4 (10)
    3136 / 2, // high 5 (11)
    3520 / 2, // high 6 (12)
    3952 / 2, // high 7 (13)
    524 / 2,  // low  1  (14)
    588 / 2,  // low  2  (15)
    660 / 2,  // low  3  (16)
    698 / 2,  // low  4  (17)
    784 / 2,  // low  5  (18)
    880 / 2,  // low  6  (19)
    988 / 2}; // low 7  (20)
const unsigned char T_Qingrigongjianchuang[] = {
    4, 9, 4, 4, 8, 9, 4, 4, 4, 7, 8,
    4, 5, 7, 5, 8, 4, 5, 7, 5, 7,
    7, 7, 8, 7, 8, 9, 11, 9, 11, 9, 7, 9, 8,
    9, 8, 8, 9, 8, 9, 9, 5, 7, 9, 8, 8, 9, 5, 4, 5, 7,
    2,
    2, 4,
    0, 0, 0, 2, 4, 2, 4, 4, 4, 4, 5, 2, 1, 0, 3, 4, 2,
    1, 1, 1, 2, 3, 3, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 2, 4, 2, 3, 4, 5, 5, 5, 5, 5, 7, 2, 4,
    3, 3, 3, 3, 5, 2, 2, 2, 2, 4, 1, 1, 1, 2, 4, 20, 0,
    0, 0, 0, 0, 2, 4, 2, 3, 4, 5, 5, 5, 5, 5, 3, 5, 4,
    3, 3, 3, 5, 2, 2, 2, 2, 4, 1, 1, 1, 2, 1, 0, 1, 0,
    0, 0, 0, 2, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 7, 5, 4,
    3, 3, 3, 5, 2, 2, 2, 2, 2, 4, 1, 1, 1, 2, 4, 20, 0,
    0, 0, 0, 0, 0, 2, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 7, 5, 4,
    3, 3, 3, 5, 2, 2, 2, 2, 2, 4, 1, 1, 1, 2, 4, 20, 0,
    0, 0, 0, 2, 4, 5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 6, 0, 0, 0, 0, 0, 2, 4, 4, 2, 4, 0,
    5, 5, 5, 5, 5, 7, 5, 4, 3, 3, 3, 5, 2, 2, 2, 2, 2, 4,
    1, 1, 1, 2, 4, 6, 0, 0, 0, 0, 2, 4, 4,
    5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 20, 0,
    0, 0, 0, 2, 4, 5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 6, 0, 0, 0, 0, 0, 2, 4, 4, 2, 4, 0,
    5, 5, 5, 5, 5, 7, 5, 4, 3, 3, 3, 5, 2, 2, 2, 2, 2, 4,
    1, 1, 1, 2, 4, 6, 0, 0, 0, 0, 2, 4, 4,
    5, 5, 3, 5, 4, 3, 3, 3, 5, 2, 4,
    1, 2, 4, 20, 0,
    0, 0, 0, 0, 2, 4,
    5, 5, 5, 7, 5, 4, 3, 3, 3, 3, 3, 1, 3, 2, 2, 2, 2, 2, 4, 2,
    1, 2, 4, 20, 0,
    3, 3, 3, 5, 2, 4, 1, 0, 0
    //----end
};
const unsigned char R_Qingrigongjianchuang[] = {
    4, 6, 2, 4, 2, 2, 4, 2, 2, 2, 8,
    2, 2, 2, 2, 8, 2, 2, 2, 2, 16,
    4, 4, 4, 4, 1, 15, 2, 2, 2, 2, 4, 4, 16,
    2, 2, 2, 2, 2, 2, 4,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, 2,
    3, 1, 2, 2, 8,
    2, 1, 1, 2, 2, 8,
    3, 1, 2, 1, 1, 8, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1,
    3, 1, 2, 2, 8,
    3, 1, 2, 2, 6, 2, 6, 2, 8,
    0};
const unsigned char T_SCUT[] = {
    0, 0, 19, 18, 4, 4, 5, 4, 4,
    5, 4, 4, 2, 0, 19, 3, 4, 1,
    2, 1, 2, 4, 4, 2, 2, 1, 2, 20, 19,
    1, 1, 1, 2, 1, 1, 19, 0, 0,
    19, 19, 0, 20, 19, 2, 2, 1, 18, 19,
    19, 19, 0, 1, 2, 2, 4, 2,
    2, 2, 4, 2, 5, 5, 4, 0, 2,
    1, 1, 1, 2, 5, 5, 3, 2, 4,
    7, 8, 5, 2, 4, 7, 8, 5, 2, 4,
    4, 5, 7, 3, 4, 2, 5, 5, 3, 2, 4, 1,
    7, 8, 5, 2, 4, 4, 7, 8, 6, 4, 5,
    4, 5, 7, 3, 4, 2, 1, 2, 20, 19, 18, 19, 0, 0,
    7, 8, 5, 2, 4, 7, 8, 5, 2, 4,
    4, 5, 7, 3, 4, 2, 5, 5, 3, 2, 4, 1,
    7, 8, 5, 2, 4, 4, 7, 8, 6, 4, 5,
    4, 5, 7, 3, 4, 2, 1, 2, 19, 0, 1,
    4, 5, 7, 3, 4, 2, 1, 2, 4, 2, 7, 5,
    7, 8, 8, 5, 7, 7};
const unsigned char R_SCUT[] = {
    2, 2, 2, 2, 2, 4, 1, 1, 16,
    3, 1, 2, 2, 2, 2, 3, 1, 16,
    4, 3, 1, 2, 6, 2, 2, 1, 1, 2, 8,
    3, 1, 2, 2, 2, 2, 2, 2, 16,
    4, 4, 1, 1, 6, 2, 2, 2, 2, 8,
    3, 1, 2, 2, 2, 4, 2, 16,
    4, 4, 2, 6, 3, 1, 2, 2, 8,
    3, 1, 2, 2, 2, 2, 2, 2, 16,
    2, 2, 2, 2, 8, 2, 2, 2, 2, 8,
    3, 1, 4, 2, 2, 4, 2, 2, 2, 1, 1, 8,
    2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 8,
    3, 1, 4, 2, 2, 4, 2, 2, 2, 2, 4, 2, 2, 16,
    2, 2, 2, 2, 8, 2, 2, 2, 2, 8,
    3, 1, 4, 2, 2, 4, 2, 2, 2, 1, 1, 8,
    2, 2, 2, 2, 6, 2, 2, 2, 2, 2, 8,
    3, 1, 4, 2, 2, 4, 3, 1, 2, 2, 8,
    3, 1, 4, 2, 2, 4, 3, 1, 2, 2, 2, 6,
    2, 4, 2, 4, 4, 30, 0};
unsigned char T_Molihua[] = { // 音符表
    3, 3, 5, 6, 8, 8, 6, 5, 5, 6, 5,
    3, 3, 5, 6, 8, 8, 6, 5, 5, 6, 5,
    5, 5, 5, 3, 5, 6, 6, 5,
    3, 2, 3, 5, 3, 2, 1, 1, 2, 1,
    3, 2, 1, 3, 2, 3, 5, 6, 8, 5,
    2, 3, 5, 2, 3, 1, 20, 19,
    20, 1, 2, 3,
    1, 2, 1, 20, 19};
unsigned char R_Molihua[] = { // 节拍表,4表示一拍，1表示1/4拍,8表示两拍
    4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 8,
    4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 8,
    4, 4, 4, 2, 2, 4, 4, 8,
    4, 2, 2, 4, 2, 2, 4, 2, 2, 8,
    2, 2, 2, 2, 6, 2, 4, 2, 2, 8,
    4, 2, 2, 2, 2, 2, 2, 8,
    4, 4, 6, 2,
    2, 2, 2, 2, 16, 0};
void Music_Play_Circal(const unsigned char *T_Song, const unsigned char *R_Song)
{
    if (++Tmp_Count < Sound_Time * 16 / 100 - 1)
        if (Tmp_Count == 1)
        {
            TIM_SetAutoreload(TIM2, 1000000 / tone[T_Song[beat]] - 1);
            TIM_PWMSetDuty(TIM2, TIM_PWMChannel_PWMA, Volume);
        }
    if (Tmp_Count == Sound_Time * 16 / 100 && Tmp_Bit++ >= R_Song[beat])
    {
        TIM_SetAutoreload(TIM2, 65535);
        TIM_PWMSetDuty(TIM2, TIM_PWMChannel_PWMA, 0);
    }
    if (Tmp_Count >= 16)
    {
        Tmp_Count = 0; // 清零 Tmp_Count
        if (Tmp_Bit++ >= R_Song[beat])
        {
            Tmp_Bit = 0;
            if (!R_Song[beat + 1])
                Music_Exit();
            else
                beat++;
        }
    }
    return;
}

void Music_Exit(void)
{
    beat = 0;
    Tmp_Bit = 0;
    Tmp_Count = 0;
    Music_Enable = 0;
    TIM_SetAutoreload(TIM2, 65535);
    TIM_PWMSetDuty(TIM2, TIM_PWMChannel_PWMA, 0);
    return;
}
void Music_Start(Typedef_Song Tmp_Song)
{
    beat = 0;
    Tmp_Bit = 0;
    Tmp_Count = 0;
    Global_Song = Tmp_Song;
    Music_Enable = 1;
    return;
}
